<template>
  <div class="dashboard">
    <el-row :gutter="20">
      <!-- 数据概览卡片 -->
      <el-col :span="6" v-for="card in cards" :key="card.title">
        <el-card shadow="hover" class="data-card">
          <div class="card-header">
            <span class="title">{{ card.title }}</span>
            <el-icon :size="24" :class="card.type">
              <component :is="card.icon" />
            </el-icon>
          </div>
          <div class="card-body">
            <div class="value">{{ card.value }}</div>
            <div class="compare">
              较上周
              <span :class="card.trend >= 0 ? 'up' : 'down'">
                {{ Math.abs(card.trend) }}%
                <el-icon>
                  <component :is="card.trend >= 0 ? 'ArrowUp' : 'ArrowDown'" />
                </el-icon>
              </span>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 图表区域 -->
    <el-row :gutter="20" class="charts-row">
      <el-col :span="16">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>访问趋势</span>
            </div>
          </template>
          <div ref="lineChartRef" class="chart"></div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>访问来源</span>
            </div>
          </template>
          <div ref="pieChartRef" class="chart"></div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import * as echarts from 'echarts'
import {
  Monitor,
  User,
  ShoppingCart,
  Money,
  ArrowUp,
  ArrowDown,
} from '@element-plus/icons-vue'

// 数据概览卡片数据
const cards = [
  {
    title: '总访问量',
    value: '102,400',
    icon: Monitor,
    type: 'primary',
    trend: 15,
  },
  {
    title: '用户数量',
    value: '81,212',
    icon: User,
    type: 'success',
    trend: 12,
  },
  {
    title: '订单数量',
    value: '9,280',
    icon: ShoppingCart,
    type: 'warning',
    trend: -8,
  },
  {
    title: '总收入',
    value: '¥138,432',
    icon: Money,
    type: 'danger',
    trend: 18,
  },
]

// 图表实例
const lineChartRef = ref<HTMLElement>()
const pieChartRef = ref<HTMLElement>()

// 初始化图表
onMounted(() => {
  // 折线图
  const lineChart = echarts.init(lineChartRef.value!)
  lineChart.setOption({
    tooltip: {
      trigger: 'axis',
    },
    xAxis: {
      type: 'category',
      data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
    },
    yAxis: {
      type: 'value',
    },
    series: [
      {
        name: '访问量',
        type: 'line',
        smooth: true,
        data: [820, 932, 901, 934, 1290, 1330, 1320],
      },
    ],
  })

  // 饼图
  const pieChart = echarts.init(pieChartRef.value!)
  pieChart.setOption({
    tooltip: {
      trigger: 'item',
    },
    legend: {
      orient: 'vertical',
      left: 'left',
    },
    series: [
      {
        name: '访问来源',
        type: 'pie',
        radius: '50%',
        data: [
          { value: 1048, name: '搜索引擎' },
          { value: 735, name: '直接访问' },
          { value: 580, name: '邮件营销' },
          { value: 484, name: '联盟广告' },
          { value: 300, name: '视频广告' },
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)',
          },
        },
      },
    ],
  })

  // 监听窗口大小变化
  window.addEventListener('resize', () => {
    lineChart.resize()
    pieChart.resize()
  })
})
</script>

<style scoped lang="scss">
.dashboard {
  .data-card {
    margin-bottom: 20px;

    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 20px;

      .title {
        font-size: 16px;
        color: #606266;
      }

      .el-icon {
        &.primary {
          color: #409eff;
        }
        &.success {
          color: #67c23a;
        }
        &.warning {
          color: #e6a23c;
        }
        &.danger {
          color: #f56c6c;
        }
      }
    }

    .card-body {
      .value {
        font-size: 24px;
        font-weight: bold;
        margin-bottom: 8px;
      }

      .compare {
        font-size: 14px;
        color: #909399;

        .up {
          color: #67c23a;
        }

        .down {
          color: #f56c6c;
        }
      }
    }
  }

  .charts-row {
    margin-bottom: 20px;

    .chart {
      height: 350px;
    }
  }
}
</style>
